<template>
  <div class="item">
    <div class="img-box">
      <img v-if="item.fileType==1" src="@/assets/file_type/directory.png">
      <img v-else-if="isImg(item)" :src="item.fileUrl">
      <img v-else-if="item.fileType==2&&item.fileSuffix" :src="fileTypeIcon[item.fileSuffix]">
      <img v-else src="@/assets/file_type/other.png">
    </div>
    <div class="item-name">
      {{item.fileName}}<span v-if="item.fileSuffix">.{{item.fileSuffix}}</span>
    </div>
    <div class="item-time">
      {{item.updateTime||item.createTime}}
    </div>
    <a-button style="margin-top: 20px" type="primary" block @click="download(item.fileUrl)" icon="download">下载</a-button>
  </div>
</template>

<script>
  import {postAction, getFileAccessHttpUrl} from "@/api/manage";
  import {isImg} from "@/utils/xinkeUtil";

  export default {
    name: "ShareFile",
    data() {
      return {
        urlCode: this.$route.query.urlCode,
        code: this.$route.query.code,
        fileTypeIcon: {
          dir: require('@assets/file_type/directory.png'),
          jpg: require('@assets/file_type/jpg.png'),
          png: require('@assets/file_type/jpg.png'),
          mp3: require('@assets/file_type/mp3.png'),
          mp4: require('@assets/file_type/mp4.png'),
          pdf: require('@assets/file_type/pdf.png'),
          txt: require('@assets/file_type/txt.png'),
          doc: require('@assets/file_type/word.png'),
          xls: require('@assets/file_type/xls.png'),
          zip: require('@assets/file_type/zip.png'),
          rar: require('@assets/file_type/zip.png'),
        },
        item: {},
        noFile:false
      }
    },
    created() {
      this.queryShareFile();
    },
    methods: {
      isImg(item){
        return isImg(item)
      },
      //分享文件信息
      queryShareFile() {
        postAction(`fileBank/share/queryShareLib?urlCode=${this.urlCode}&code=${this.code}`).then(res => {
          if (res.success) {
            if(res.result.crdFileLib){
              this.item = res.result.crdFileLib
            }else{
              this.noFile = true;
            }
          } else {
            this.$message.error(res.message);
          }
        })
      },
      download(url) {
        if (!url) {
          this.$message.warning("文件不存在，无法下载");
          return;
        }
        if (url.indexOf(",") > 0) {
          url = url.substring(0, url.indexOf(","))
        }
        url = getFileAccessHttpUrl(url);
        window.open(url);
      }
    }
  }
</script>

<style scoped lang="less">
  .item {
    display: inline-block;
    text-align: center;
    margin: auto;
    display: block;
    position: absolute;
    top: 50%;
    left: 50%;
    width: 400px;
    height: 300px;
    margin: -150px 0 0 -200px;
    z-index: 1;

    &-name {
      font-size: 16px;
      color: #333;
      margin-top: 15px;
    }

    &-time {
      color: #afb3bf;
      font-size: 14px;
      margin-top: 10px;
    }
  }

</style>